﻿using System.IO;
using Newtonsoft.Json;
using TimeMachine.Engine.Vcs;
using TimeMachine.Engine.Vcs.Svn;

namespace TimeMachine.Engine.Agents
{
	public class ProjectSettingsSerializer : IProjectSettingsSerializer
	{
		public string Serialize(IProjectSettings settings)
		{
			return JsonConvert.SerializeObject(settings, Formatting.Indented);
		}

		public IProjectSettings Deserialize(string text, string dataDirectoryPath)
		{
			var settings = JsonConvert.DeserializeObject<ProjectSettings>(
				text,
				new CustomConverter<IVcsRepo, SvnRepo>(),
				new CustomConverter<ISourcesSettings, SourcesSettings>()
				);
			if(!Path.IsPathRooted(settings.StoragePath))
				settings.StoragePath = dataDirectoryPath + Path.DirectorySeparatorChar + settings.StoragePath;
			return settings;
		}
	}
}